﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
using Zimmerreservierung.Controller;

namespace Zimmerreservierung.Forms
{
    public partial class FormKundeNeu : Form
    {
        protected KundenController kundenController;

        public FormKundeNeu()
        {
            InitializeComponent();

            this.kundenController = KundenController.getInstance();

            this.btnSave.DialogResult = DialogResult.OK;
            this.btCancel.DialogResult = DialogResult.Cancel;
            this.txtRabatt.Text = "0";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtName.Text.Length == 0 || txtRechnungsaddresse.Text.Length == 0 || txtTelefonnummer.Text.Length == 0)
                {
                    MessageBox.Show("Bitte geben Sie Namen, Rechnungsadresse und Telefonnumer an!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    DialogResult = System.Windows.Forms.DialogResult.None;
                }
                else
                {
                    string name = txtName.Text;
                    string rAddr = txtRechnungsaddresse.Text;
                    string fName = txtFirmenname.Text;
                    string telNr = txtTelefonnummer.Text;
                    string faxNr = txtFaxnummer.Text;
                    string eMail = txtEmail.Text;
                    string web = txtWeb.Text;
                    string notiz = txtNotiz.Text;
                    string rab = txtRabatt.Text;
                    int rabatt = 0;
                    string strRabatt = string.Empty;

                    if (!string.IsNullOrEmpty(rab))
                    {
                        if (Int32.TryParse(rab, out rabatt))
                        {
                            double dbRabatt = rabatt / 100.0;
                            strRabatt = Convert.ToString(dbRabatt).Replace(',', '.');
                        }
                        else
                        {
                            MessageBox.Show("Rabatt muss eine Zahl sein!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            DialogResult = System.Windows.Forms.DialogResult.None;
                            return;
                        }
                    }
                    StoreKunde(name, rAddr, fName, strRabatt, telNr, faxNr, eMail, web, notiz);
                    this.Close();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("An unexpected Exception occured. Inform maintenance personal. ID:0012x3", "Error", MessageBoxButtons.OK);
                this.Close();
            }

        }

        internal virtual void StoreKunde(string name, string rAddr, string fName, string strRabatt, string telNr, string faxNr, string eMail, string web, string notiz)
        {
            try
            {
                kundenController.StoreKunde(name, rAddr, fName, strRabatt, telNr, faxNr, eMail, web, notiz);
                MessageBox.Show("Kunde erfolgreich gespeichert.");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
